//
//  BZThemeMacor.h
//  Pods
//
//  Created by Sings on 2022/8/18.
//

#import <objc/runtime.h>
#import <objc/message.h>

#ifndef BZThemeMacor_h
#define BZThemeMacor_h

typedef void(^BZThemeBindBlock)(void);
//
//static __inline__ __attribute__((always_inline)) const void * invokeSelectorReturn(id target, NSString *strSel) {
//    SEL sel = NSSelectorFromString(strSel);
//    if ([target respondsToSelector:sel]) {
//        return ((void  * (*)(id, SEL))(void *) objc_msgSend)((id)target, sel);
//    }else {
//        sel = NSSelectorFromString(@"BZTheme_colorWithKey:");
//        return ((void  * (*)(id, SEL, NSString *))(void *) objc_msgSend)((id)target, sel, strSel);;
//    }
//
//}

// 主题颜色
#define BZTHEME_COLOR(...) \
    BZ_COUNT_EQ1(BZ_ARG_COUNT(__VA_ARGS__)) \
    (BZTHEME_COLOR_KEY_(__VA_ARGS__))\
    (BZTHEME_COLOR_BIND_(__VA_ARGS__))

// 主题颜色 CGCOLOR
#define BZTHEME_CGCOLOR(...) \
    BZ_COUNT_EQ1(BZ_ARG_COUNT(__VA_ARGS__)) \
    (BZTHEME_CGCOLOR_KEY_(__VA_ARGS__))\
    (BZTHEME_CGCOLOR_BIND_(__VA_ARGS__))

// 主题图片
#define BZTHEME_IMAGE(...) \
    BZ_COUNT_EQ1(BZ_ARG_COUNT(__VA_ARGS__)) \
    (BZTHEME_IMAGE_KEY_(__VA_ARGS__))\
    (BZTHEME_IMAGE_BIND_(__VA_ARGS__))

// 主题BLOCK
#define BZTHEME_BLOCK(OBJ, BLOCK) \
    [[BZThemeManager sharedInstance] bindBlockWithTarget:OBJ Block:BLOCK]

// 主题方法
#define BZTHEME_SEL(OBJ, SEL) \
    [[BZThemeManager sharedInstance] bindSelWithTarget:OBJ Sel:SEL]

// 主题json字符串
#define BZTHEME_STR(KEY) \
    BZThemeKeyToStr(KEY)

//#define BZTHEME_REFRESH() \
//    [[BZThemeManager sharedInstance] refresh]



#define BZ_KEY_PATH(OBJ, PATH) \
    (((void)(NO && ((void)OBJ.PATH, NO)), # PATH))

#define BZTHEME_IMAGE_BIND_(KEY, OBJ, PATH) \
    [[BZThemeManager sharedInstance] bindImageWithTarget:OBJ KeyPath:@BZ_KEY_PATH(OBJ, PATH) ToKey:KEY]
#define BZTHEME_IMAGE_KEY_(KEY) \
    [UIImage BZTheme_image:KEY]

#define BZTHEME_COLOR_BIND_(KEY, OBJ, PATH) \
    [[BZThemeManager sharedInstance] bindColorWithTarget:OBJ KeyPath:@BZ_KEY_PATH(OBJ, PATH) ToColorPath:KEY]

#define BZTHEME_COLOR_KEY_(KEY) \
    [UIColor BZTheme_color:KEY]

#define BZTHEME_CGCOLOR_BIND_(KEY, OBJ, PATH) \
    [[BZThemeManager sharedInstance] bindCGColorWithTarget:OBJ KeyPath:@BZ_KEY_PATH(OBJ, PATH) ToColorPath:KEY]
#define BZTHEME_CGCOLOR_KEY_(KEY) \
    ([UIColor BZTheme_color:KEY].CGColor)

#define BZ_CONSUME_(...)
#define BZ_EXPAND_(...) __VA_ARGS__

#define BZ_CONCAT(A, B) \
        BZ_CONCAT_(A, B)

#define BZ_CONCAT_(A, B) A ## B

#define BZ_ARG_COUNT(...) BZ_ARG_COUNT_(0,##__VA_ARGS__,5,4,3,2,1,0)
#define BZ_ARG_COUNT_(...) BZ_ARG_COUNT_N(__VA_ARGS__)
#define BZ_ARG_COUNT_N(_0,_1,_2,_3,_4,_5,N,...) N
#define BZ_COUNT_EQ1(COUNT) BZ_CONCAT(BZ_COUNT_EQ1_, COUNT)
#define BZ_COUNT_EQ1_0(...) BZ_EXPAND_
#define BZ_COUNT_EQ1_1(...) __VA_ARGS__ BZ_CONSUME_
#define BZ_COUNT_EQ1_2(...) BZ_EXPAND_
#define BZ_COUNT_EQ1_3(...) BZ_EXPAND_
#define BZ_COUNT_EQ1_4(...) BZ_EXPAND_
#define BZ_COUNT_EQ1_5(...) BZ_EXPAND_

#define BZ_INLINE __inline__ __attribute__((always_inline))
#define BZ_EXTERN extern


#endif /* BZThemeMacor_h */
